/*
 * Copyright (c) 2011 Junichiro Okuno
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of Junichiro Okuno.
 */

package tcp_net.mail.pop3;

import tcp_net.*;
import tcp_net.management.EmailServerManager;
import tcp_net.mail.pop3.debug.DebugInfo;
import java.util.ArrayList;

/**
	POP3 Server class provides functionalities specific to POP3
	@author J. Okuno
*/
public class POP3Server extends Server
{
	/**
		Debug information
	*/
	private final static boolean DEBUG = DebugInfo.POP3_SERVER_DEBUG;
	
	/**
		A default server port
	*/
	public static final int POP3_PORT = 110;
	
	/**
		An email server manager
	*/
	private EmailServerManager email_manager;
	
	/**
		Init POP3Server
		@param port a port number of this server
		@param numThreads a number of handlers to run
		@param man an email server manager
	*/
	public POP3Server(int port, int numThreads, EmailServerManager man)
	{
		super(port, numThreads);	
		email_manager = man;
		DebugInfo.print(DEBUG,"POPServer created. Port: "+port+" No. of handlers: "+numThreads);
	}
	
	/**
		Init POP3Server with default port number: 110
		@param numThreads a number of handlers to run
		@param man an email server manager
	*/
	public POP3Server(int numThreads, EmailServerManager man)
	{
		this(POP3_PORT, numThreads, man);
	}
	
	/**
		This method starts running POP3 server with its Handler
	*/
	public synchronized void startServer()
	{
		handlers.clear();
		
		for(int i=0; i < no_of_threads; i++)
		{
			handlers.add(new POP3Handler(email_manager, servSock, i));
		}
		
		runHandlers(handlers);
	}
}